package org.csu.mall.controller;

import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Bank;
import org.csu.mall.service.IBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/bank/")
public class BankController {
    @Autowired
    private IBankService bankService;

    @GetMapping("check_cart_no")
    @ResponseBody
    public UniformResponse<String> checkCartNo(String id){
        UniformResponse<String> response = bankService.checkCartNo(id);
        return response;
    }
    @PostMapping("add_bank")
    @ResponseBody
    public UniformResponse<Bank> addBank(Bank bank, HttpSession session){
        UniformResponse<Bank> response = bankService.addBank(bank);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.BANK,response.getData());
        }
        return response;
    }
    @PostMapping("delete_bank")
    @ResponseBody
    public UniformResponse<String> deleteBank(String id){
        UniformResponse<String> response = bankService.deleteBank(id);
        return response;
    }
    @GetMapping("check_bank_password")
    @ResponseBody
    UniformResponse<String> checkBankPassword(String id,String paymentPassword){
        UniformResponse<String> response = bankService.checkBankPassword(id,paymentPassword);
        return response;
    }
    @GetMapping("get_bank_info_one")
    @ResponseBody
    UniformResponse<Bank> getBankInfoOne(String id,HttpSession session){
        UniformResponse<Bank> response = bankService.getBankInfoOne(id);
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.BANK,response.getData());
        }
        return response;
    }
    @GetMapping("get_bank_info")
    @ResponseBody
    UniformResponse<List<Bank>> getBankInfo(HttpSession session){
        UniformResponse<List<Bank>> response = bankService.getBankInfo();
        if(response.isSuccess()){
            session.setAttribute(CONSTANT.BANKLIST,response.getData());
        }
        return response;
    }
    @PostMapping("update_bank")
    @ResponseBody
    UniformResponse<Bank> updateBank(Integer id,String phone,HttpSession session)
    {
        UniformResponse<Bank> response=bankService.updateBank(id, phone);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.UPDATEBANK,response.getData());
        }
        return response;
    }

}
